package com.first.community.controller;


import com.first.community.cache.TagCache;
import com.first.community.dto.QuestionDTO;
import com.first.community.mapper.QuestionMapper;
import com.first.community.model.Question;
import com.first.community.model.User;
import com.first.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PublishController {


    @Autowired
    private QuestionService questionService;


    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") Long id,
                       Model model){

        QuestionDTO questionDTO = questionService.getById(id);
        model.addAttribute("title",questionDTO.getTitle());
        model.addAttribute("description",questionDTO.getDescription());
        model.addAttribute("tag",questionDTO.getTag());
        model.addAttribute("id",questionDTO.getId());
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @GetMapping("/publish")
    public String Publish(Model model){

        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(name="title") String title,
            @RequestParam(name="description")String description,
            @RequestParam(name="tag") String tag,
            @RequestParam(name="id",required = false) Long id,
            Model model,
            HttpServletRequest request
    ){
        //回显在页面上
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());

        User user = (User)request.getSession().getAttribute("user");
        if(user==null){
            model.addAttribute("error","用户未登录");
            return "/publish";
        }

        //判断前端是否填充了必要字段
        if(title==null || title==""){
            model.addAttribute("error","title不为空");
            return  "publish";
        }
        if(description==null || description==""){
            model.addAttribute("error","描述不为空");
            return  "publish";
        }
        if(tag==null || tag==""){
            model.addAttribute("error","标签不为空");
            return  "publish";
        }


        //写入数据库
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setGmtCreate(System.currentTimeMillis());
        question.setGmtModified(question.getGmtCreate());
        question.setCreator(user.getId());
        question.setId(id);

        //评论数等默认值为0
        question.setViewCount(0);
        question.setCommentCount(0);
        question.setLikeCount(0);

        questionService.CreateOrUpdate(question);
        return  "redirect:/";
    }
}
